package org.shuma.user.mng.controller;

import org.shuma.user.mng.domain.vo.AuthVo;
import org.shuma.user.mng.service.AuthService;
import org.shuma.user.mng.constant.RegularConstant;
import org.shuma.user.mng.vaild.group.AuthGroup;
import org.shuma.user.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;

/**
 * @author zhao haitao
 * @date 2024/06/17 1:18 AM
 */
@Controller
@RestController
@Validated
public class AuthController {
    @Autowired
    private AuthService authService;

    @GetMapping("/code")
    public Result sendVerifyCode(@RequestParam("email") @Valid @Email(regexp = RegularConstant.EMAIL, message = "邮箱格式不正确") @NotNull String email) {
        return authService.sendVerifyCode(email);
    }

    @PostMapping("/register")
    public Result register(@RequestBody @Validated(AuthGroup.register.class) AuthVo auth) {
        return authService.register(auth);
    }

    @PostMapping("/login")
    public Result login(@RequestBody @Validated(AuthGroup.login.class) AuthVo auth) {
        return authService.login(auth);
    }

    @PostMapping("/logout")
    public Result logout(@RequestHeader("Authorization") @NotNull String token) {
        return authService.logout(token);
    }

    @GetMapping("/token")
    public Result getNewToken(@RequestHeader("Authorization") @NotNull String token) {
        return authService.getNewToken(token);
    }
}
